package com.example.demo810.api;

import com.example.demo810.cfg.IgnoreSecurity;
import com.example.demo810.event.AsyncNoticeEvent;
import com.example.demo810.event.EventPublish;
import com.example.demo810.vo.AjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author lzh
 * @date 2020/8/11 - 18:28
 */
@RestController
@Slf4j
@Api(tags = "applicationEvent事件测试")
public class EventController {

    @Autowired
    EventPublish eventPublish;


    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    /**
     * 异步事件
     * https://blog.csdn.net/superylcfly/article/details/103978422
     * @param message
     */
    @ApiOperation("异步事件测试")
    @IgnoreSecurity(name = "忽略token校验")
    @GetMapping("/notice/{message}")
    public void notice(@PathVariable(name = "message") String message) {
        log.info("begin >>>>>>");
        applicationEventPublisher.publishEvent(new AsyncNoticeEvent(message));
        log.info("end <<<<<<");
    }

    /**
     * 发布事件
     * 相当于发信号
     * https://blog.csdn.net/weixin_37910453/article/details/90117011?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.add_param_isCf
     * @return
     */
    @ApiOperation("发布事件")
    @IgnoreSecurity(name = "忽略token校验")
    @GetMapping("/publish")
    public AjaxResponse<Void> test() {

        System.out.println("开始发布1个事件"+new Date().toString());

        eventPublish.publish("发布事件");
        return AjaxResponse.successMsg("发布事件测试,请观察java控制台");
    }
}
